<template>
    <div>

        <!-- 筛选条件 -->
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
            <el-form-item label="网关" prop="gatewayId">
                <gateway-select @changeGateway="changeGateway"/>
            </el-form-item>
            <el-form-item label="信息点" prop="sensorId">
                <el-select v-model="queryParams.sensorId"
                           size="small"
                           @change="changeSensor"
                           clearable
                >
                    <el-option
                        v-for="item in sensorList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    >
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="是否通知" prop="status">
                <el-select v-model="queryParams.status"
                           size="small"
                           @change="changeStatus"
                           clearable
                >
                    <el-option
                        v-for="item in statusList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    >
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="是否处理" prop="isProcess">
                <el-select v-model="queryParams.isProcess"
                           size="small"
                           @change="changeIsProcess"
                           clearable
                >
                    <el-option
                        v-for="item in isProcessList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    >
                    </el-option>
                </el-select>
            </el-form-item>
        </el-form>

        <el-button
            plain
            :disabled="multiple"
            @click="processHandler"
        >设为已处理
        </el-button>

        <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="序号" align="center" type="index" :index="calculateIndex"/>
            <el-table-column label="网关" align="center" prop="gatewayName"/>
            <el-table-column label="信息点" align="center" prop="sensorName"/>
            <el-table-column label="报警时间" align="center" prop="warnTime">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.warnTime) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="是否通知" align="center" prop="status">
                <template slot-scope="scope">
                    <el-tag v-if="scope.row.status==='0'" type="info">否</el-tag>
                    <el-tag v-if="scope.row.status==='1'" type="success">是</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="通知时间" align="center" prop="noticeTime">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.noticeTime) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="报警值" align="center" prop="sensorVal"/>
            <el-table-column label="触发报警规则" align="center" prop="warnParam"/>
            <el-table-column label="是否处理" align="center" prop="isProcess">
                <template slot-scope="scope">
                    <el-tag v-if="scope.row.isProcess==='0'" type="info">否</el-tag>
                    <el-tag v-if="scope.row.isProcess==='1'" type="success">是</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="处理时间" align="center" prop="processTime">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.processTime) }}</span>
                </template>
            </el-table-column>
        </el-table>

        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />
    </div>
</template>

<script>
import { listSensorByGatewayId, pageWarnList, processWarn } from '@/api/equipment/sensor'

import gatewaySelect from '@/views/common/GatewaySelect.vue'

export default {
    name: 'index',
    data() {
        return {
            showSearch: true,
            dataList: [],
            total: 0,
            loading: false,
            multiple: true,
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                gatewayId: null,
                status: null,
                isProcess: null
            },
            ids: [],
            statusList: [
                { 'value': '0', 'label': '未通知' },
                { 'value': '1', 'label': '已通知' }
            ],
            isProcessList: [
                { 'value': '0', 'label': '未处理' },
                { 'value': '1', 'label': '已处理' }
            ],
            sensorList: []
        }
    },
    components: {
        gatewaySelect
    },
    created() {
        this.getList()
    },
    methods: {
        calculateIndex(index) {
            const pageNum = this.queryParams.pageNum
            const pageSize = this.queryParams.pageSize
            return index + 1 + (pageNum - 1) * pageSize
        },
        getList() {
            this.loading = true
            pageWarnList(this.queryParams).then(response => {
                this.dataList = response.rows
                this.total = response.total
                this.loading = false
            })
        },
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.multiple = !selection.length
        },
        processHandler() {
            processWarn({ ids: this.ids }).then(response => {
                this.$message.success('成功')
                this.getList()
                this.$emit('getCount')
            })
        },
        // 接收子组件传递的消息
        changeGateway(obj) {
            let gatewayId = obj.gatewayId
            this.queryParams.gatewayId = gatewayId
            this.getList()

            // console.log('gatewayId ', gatewayId)

            this.sensorList = []
            // 点击选择框右边的叉时，传回的值为''，而不是null
            if (gatewayId == null || gatewayId === '') {
                return
            }
            listSensorByGatewayId(gatewayId).then(response => {
                for (let i = 0; i < response.data.length; ++i) {
                    let obj = {}
                    obj['value'] = response.data[i]['sensorId']
                    obj['label'] = response.data[i]['sensorName']
                    this.sensorList.push(obj)
                }
            })
        },
        changeSensor(val) {
            this.getList()
        },
        changeStatus(val) {
            this.getList()
        },
        changeIsProcess(val) {
            this.getList()
        }
    }
}
</script>

<style scoped>

</style>
